import std::IO;
import std::Type;
import std::TIME;
import std::OS;

import util::PixelReader;
import util::PixelPainter;

class Shuimu
{
	x->int;
	y->int;
	f1->int[];
	f2->int[];
	f3->int[];

	public Shuimu(x->int, y->int)
	{
		this.x = x;
		this.y = y;
		f1 = PixelReader.read("frame/1.txt");
		f2 = PixelReader.read("frame/2.txt");
		f3 = PixelReader.read("frame/3.txt");
	}

	public draw(){
		if(f1 != null && f2 != null && f3 != null)
		{
			frags->int[][] = new int[5][0]{f1,f2,f3,f2,f1};
			while(true){
				i->int = 0;
				while(i<frags.length){
					PixelPainter.redraw();
					PixelPainter.draw(x,y,frags[i++]);
					std::TIME.sleep(200);	//ms
					co_yield null;
				}
			}
		}
	}

	public move(offx->int, offy->int)
	{
		x+=offx;
		y+=offy;
		if(x<0) x=0;
		if(10<x) x=10;
		if(y<5) y=5;
		if(50<y) y=50;
	}
}

public class Main
{
	public static control(shuimu->Shuimu)
	{
		t0->long = TIME.clock();
		while(true){
			t1->long = TIME.clock();
			if(t0+200<=t1)
			{
				n0->int = 2-OS.rand()%5;
				n1->int = 1-OS.rand()%3;
				shuimu.move(n0,n1);
				t0 = t1;
			}
			co_yield null;
		}
	}

	public static main()
	{
		shuimu->Shuimu = new Shuimu(5,30);
		co_start Main.control(shuimu);
		shuimu.draw();
	}
}